package co.yixiang.modules.shop.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.aop.NoRepeatSubmit;
import co.yixiang.modules.shop.domain.YxStoreReport;
import co.yixiang.modules.shop.service.YxStoreReportService;
import co.yixiang.modules.shop.service.dto.YxStoreReportQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author fanfan
 * @date 2022-02-25 16:25
 */

@Api(tags = "通知公告管理")
@RestController
@RequestMapping("/api/yxStoreReport")
public class StoreReportControll {

    private final YxStoreReportService yxStoreReportService;
    private final IGenerator generator;

    public StoreReportControll(YxStoreReportService yxStoreReportService, IGenerator generator) {
        this.yxStoreReportService = yxStoreReportService;
        this.generator = generator;
    }

    @GetMapping
    @Log("查询通知公告管理")
    @ApiOperation("查询通知公告管理")
    @PreAuthorize("@el.check('YXSTOREREPORT_SELECT')")
    public ResponseEntity<Object> getYxStoreReport(YxStoreReportQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(yxStoreReportService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @NoRepeatSubmit
    @PostMapping
    @Log("新增通知公告")
    @ApiOperation("新增通知公告")
    @PreAuthorize("@el.check('YXSTOREREPORT_ADD')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxStoreReport resources) {
        return new ResponseEntity<>(yxStoreReportService.save(resources), HttpStatus.CREATED);
    }

    @NoRepeatSubmit
    @PutMapping
    @Log("修改通知公告")
    @ApiOperation("修改通知公告")
    @PreAuthorize("hasAnyRole('YXSTOREREPORT_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxStoreReport resources) {
        yxStoreReportService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @NoRepeatSubmit
    @Log("删除通知公告")
    @ApiOperation("删除通知公告")
    @PreAuthorize("@el.check('YXSTOREREPORT_DEL')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxStoreReportService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
}